<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="品类名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入品类名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="归属分类" prop="teamCategoryId">
        <el-input
          v-model="queryParams.teamCategoryId"
          placeholder="请输入归属分类"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="筛选标签" prop="tags">
        <el-input
          v-model="queryParams.tags"
          placeholder="请输入筛选标签"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:teamCategoryItem:add']"
        >新增</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="teamCategoryItemList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="id" align="center" prop="id" /> -->
      <el-table-column label="品类名称" align="center" prop="name" />
      <el-table-column label="归属分类" align="center" prop="teamCategoryId" />
      <el-table-column label="收费方式" align="center" prop="tollType" :formatter="hargingMethodFormat"/>
      <el-table-column label="排序" align="center" prop="sortId" />
      <el-table-column label="添加时间" align="center" prop="createDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:teamCategoryItem:edit']"
          >编辑</el-button>
          <el-button
            size="mini"
            type="text"
            @click="showLevel(scope.row)"
          >段位</el-button>
          <el-button
            size="mini"
            type="text"
            @click="showTag(scope.row)"
          >标签</el-button>
          <el-button
            size="mini"
            type="text"
            @click="showPosition(scope.row)"
          >位置</el-button>
          <el-button
            size="mini"
            type="text"
            @click="showRegion(scope.row)"
          >大区</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:teamCategoryItem:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改组队品类对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="40%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="品类名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入品类名称" />
        </el-form-item>
        <el-form-item label="归属分类" prop="teamCategoryId">
          <el-select v-model="form.teamCategoryId">
            <el-option v-for="item in teamCategoryOptions" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="品类图标" prop="icon">
          <SingleUpload :avatar="form.icon" @emitSetUrl="setUrl" />
        </el-form-item>
        <el-form-item label="收费方式">
          <el-select v-model="form.tollType" placeholder="请选择收费方式">
            <el-option v-for="dict in chargingMethodOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
          </el-select>
        </el-form-item>
        <el-form-item label="筛选标签" prop="tags">
          <el-input v-model="form.tags" placeholder="请输入筛选标签" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <Level ref="Level" v-if="levelVis" />
    <Region ref="Region" v-if="regionVis" />
    <Tag ref="Tag" v-if="tagVis" />
    <Position ref="Position" v-if="positionVis" />

  </div>
</template>

<script>
import { listTeamCategoryItem, getTeamCategoryItem, delTeamCategoryItem, addTeamCategoryItem, updateTeamCategoryItem, exportTeamCategoryItem } from "@/api/system/teamCategoryItem";
import { loadTeamCategory } from "@/api/system/teamCategory";
import SingleUpload from '@/components/Upload/SingleUpload'
import Level from "./level"
import Region from "./region"
import Tag from "./tag"
import Position from "./position"
export default {
  name: "TeamCategoryItem",
  components:{
    Level,
    Region,
    Tag,
    Position,
    SingleUpload
  },
  data() {
    return {
      levelVis:false,
      regionVis:false,
      tagVis:false,
      positionVis:false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 组队品类表格数据
      teamCategoryItemList: [],
      teamCategoryOptions: [],
      chargingMethodOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        teamCategoryId: undefined,
        icon: undefined,
        tags: undefined,
        tollType: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "品类名称不能为空", trigger: "blur" }
        ],
        teamCategoryId: [
          { required: true, message: "归属分类不能为空", trigger: "blur" }
        ],
        icon: [
          { required: true, message: "品类图标不能为空", trigger: "blur" }
        ],
        tollType: [
          { required: true, message: "收费方式不能为空", trigger: "blur" }
        ],
        sortId: [
          { required: true, message: "排序不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("charging_method").then(response => {
      this.chargingMethodOptions = response.data;
    });
    loadTeamCategory().then(response => {
      this.teamCategoryOptions = response.data;
    });
  },
  methods: {
     // 类型字典翻译
    hargingMethodFormat(row, column) {
      return this.selectDictLabel(this.chargingMethodOptions, row.tollType);
    },
    setUrl(data){
      this.form.icon = data.url
    },
    showLevel(row){
      this.levelVis = true
      this.$nextTick(() => {
        this.$refs.Level.init(row)
      })
    } , 
    showPosition(row){
      this.positionVis = true
      this.$nextTick(() => {
        this.$refs.Position.init(row)
      })
    } , 
    showRegion(row){
      this.regionVis = true
      this.$nextTick(() => {
        this.$refs.Region.init(row)
      })
    } , 
    showTag(row){
      this.tagVis = true
      this.$nextTick(() => {
        this.$refs.Tag.init(row)
      })
    } , 
    /** 查询组队品类列表 */
    getList() {
      this.loading = true;
      listTeamCategoryItem(this.queryParams).then(response => {
        this.teamCategoryItemList = response.rows;
        this.total = parseInt(response.total);
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        name: undefined,
        teamCategoryId: undefined,
        icon: undefined,
        tags: undefined,
        tollType: undefined,
        sortId: undefined,
        createDate: undefined,
        createBy: undefined,
        status: "0"
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加组队品类";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getTeamCategoryItem(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改组队品类";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateTeamCategoryItem(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addTeamCategoryItem(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除组队品类编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delTeamCategoryItem(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有组队品类数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportTeamCategoryItem(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>